import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class mobileterminal implements ActionListener
{		
	public Font f = new Font("Times new roman" , Font.BOLD , 25);	
	public Font f1 = new Font("Times new roman", Font.BOLD , 18);
	public Font f2 = new Font("Times new roman", Font.BOLD , 16);
	public Font f3 = new Font("Times new roman", Font.BOLD , 23);
	public JLabel T0= new JLabel("Mobile Terminal");
	public JLabel T1= new JLabel("Select the Method");
	public JLabel T2= new JLabel("Select the File    :");
	public JLabel T3= new JLabel("File Path            :");
	public JLabel T4= new JLabel();
	public JLabel T5= new JLabel("Select The Route");
	public JLabel T6= new JLabel("FA    -Foriegn Agent");
	public JLabel T7= new JLabel("GFA   -Gateway Foriegn Agent");
public JLabel T11= new JLabel("MOBILITY  MANAGEMENT  APPROACHES  FOR  MOBILE  ");
	
	public JLabel T12= new JLabel("IP  NETWORKS : PERFORMANCE  COMPARISON ");
	
	public JCheckBox chk1=new JCheckBox("");
	public JComboBox TAreu = new JComboBox();	
	public JButton btn = new JButton("Submit");
	public JTextArea tf = new JTextArea();
	public JScrollPane pane = new JScrollPane();
	public JButton btn1 = new JButton("Browse");
	
	 JLabel imageLabel = new JLabel();
	    JLabel image= new JLabel();
	    JLabel imageLabel1 = new JLabel();
	    JLabel image1= new JLabel();
	    
	    JLabel imageLabel2 = new JLabel();
	    JLabel image12= new JLabel();
	    
	    JPanel jp=new JPanel();
		JPanel jp1=new JPanel();
		
	public JFrame jf;
	public Container c;
		
	mobileterminal()
	{			
		jf = new JFrame("mobileterminal");
		c = jf.getContentPane();
		c.setLayout(null);
		jf.setSize(1080,1080);
		 
		T11.setBounds(180,-3,1200,50);
		T12.setBounds(230,29,1000,50);
		T12.setForeground(Color.MAGENTA);
		T11.setForeground(Color.MAGENTA);
		 T11.setFont(f);
		 T12.setFont(f);
		
		c.setBackground(new Color(2,62,72));
		T0.setBounds(440,60,350,45);
	    T0.setFont(f);
	   
	    T0.setForeground(Color.RED);
		T2.setBounds(205, 150, 250,45);
		T2.setFont(f3);
		T2.setForeground(Color.GREEN);
		
		T3.setBounds(205, 210, 250,45);
		T3.setFont(f3);
		T3.setForeground(Color.GREEN);
		
		T4.setBounds(400, 210, 950,45);
		T4.setFont(f2);
		T4.setForeground(Color.ORANGE);
		
		T5.setBounds(660, 145, 300,45);
		T5.setFont(f3);
		T5.setForeground(Color.CYAN);
		
		T6.setBounds(760, 70, 400,35);
		T6.setFont(f2);
		T6.setForeground(Color.RED);
		
		T7.setBounds(760, 110, 400,35);
		T7.setFont(f2);
		T7.setForeground(Color.MAGENTA);
	  	    
		btn1.setBounds(590,153,200,40);
		btn1.setFont(f3);
		btn1.setBackground(Color.WHITE);
		btn1.setForeground(new Color(120,0,0));
		
		jp.setBounds(60,112,920,540);
		jp.setBackground(Color.GRAY);
		
		jp1.setBounds(55,107,930,550);
		
		T1.setBounds(300,550,250,45);
		TAreu.setBounds(650,550,200,35);
		btn.setBounds(440,580,200,40);
		T1.setFont(f3);
		T1.setForeground(new Color(0,120,0));
		btn.setFont(f3);
		btn.setBackground(Color.WHITE);
		btn.setForeground(new Color(120,0,0));
		TAreu.addItem("Mesh Route");
		TAreu.addItem(" Node ");
		TAreu.setFont(f3);
		TAreu.setBackground(Color.WHITE);
		TAreu.setForeground(Color.BLUE);
		TAreu.setFont(f);
		pane.setBounds(205, 280, 590, 250);
		
		tf.setColumns(20);
		
		tf.setForeground(Color.MAGENTA);
		tf.setFont(f1);
		tf.setRows(10);
		tf.setName("tf");
		pane.setName("pane");
		pane.setViewportView(tf);
	
		btn1.addActionListener(this);
		btn.addActionListener(this);
		
		btn1.setMnemonic(KeyEvent.VK_B);
		btn.setMnemonic(KeyEvent.VK_S); 
		jf.show();
	 	
		c.add(btn);
		c.add(T2);
		c.add(T3);
		c.add(T4);
		c.add(T11);
		c.add(T12);
		c.add(pane, BorderLayout.CENTER);
		c.add(btn1);
		c.add(T0);
				
		c.add(imageLabel1);
		
		c.add(jp);
		c.add(jp1);
		
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent win) {
				System.exit(0);
			}
		});
		
	}		

		public void actionPerformed(ActionEvent e)
		 {		
		    	DataInputStream input;

		    	BufferedInputStream bis;
		    	BufferedOutputStream bos = null;
		    	BufferedWriter writer = null;
		    	int in;
		    	String str="MobileTerminal,Foriegn Agent  A,Gateway FA  A";
		    	byte[] byteArray=str.getBytes();
		    	
		    			    			 
			    String strLine = null;
			    String newline = "\n";
			    if (e.getSource()== btn1)
			    {
	        									
				    
				 JFileChooser chooser = new JFileChooser();
			    			    
			    try {
			        
			        File f = new File(new File("filename.txt").getCanonicalPath());
			    
			        chooser.setSelectedFile(f);
			        }      catch (IOException e1) {
			        }
			    		  
			      chooser.showOpenDialog(btn1);
			      int retval = chooser.showOpenDialog(btn1);
                  if (retval == JFileChooser.APPROVE_OPTION){
                  File field = chooser.getSelectedFile();
                  T4.setText(field.getAbsolutePath());
                 }		    
			    
			            File curFile = chooser.getSelectedFile();
			        try{
				    
				        FileInputStream fstream = new FileInputStream(curFile);
				   
				        DataInputStream ins = new DataInputStream(fstream);
				        BufferedReader br = new BufferedReader(new InputStreamReader(ins));
				 
			    		
				        StringBuffer buffer = new StringBuffer();
				    while ((strLine = br.readLine()) != null)   {
				     
				        System.out.println (strLine);
				        buffer.append(strLine+ "\n");
				          
				      
				    }
				       tf.setText(buffer.toString());
				    
				    }catch (Exception e1){
				      System.err.println("Error: " + e1.getMessage());
				    }
                Socket client = null;
					
					int Port = 1111 ;
									
					try {
						client = new Socket("127.0.0.1", 1111 );
						
			    		bos = new BufferedOutputStream(client.getOutputStream()); 
			    		//byteArray = tf.getText().getBytes();
			    		bos.write(byteArray, 0, byteArray.length);
			    		bos.flush();
			    		bos.close();
			    		client.close();
					} catch (UnknownHostException e1) {
						
						e1.printStackTrace();
					} catch (Exception e1) {}
						
					finally {
						if (bos != null) {
							try {
								bos.close();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}
						try {
							client.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
							
					}
				    
			 }
			 if(e.getSource()== btn)
			 {
				 
				 
				 JFrame frame = new JFrame();
				    String message = "Please Type any one the following Foreign Agents FA1 ,FA2,FA3,FA4 ";
				    String text = JOptionPane.showInputDialog(frame, message);
				    Socket client = null;
				    System.out.println(text);
				    if (text.equals("FA1"))
				    {
				    
					
				
					int Port = 8587;
									
					try {
						client = new Socket("127.0.0.1", Port);
						
			    		bos = new BufferedOutputStream(client.getOutputStream()); 
			    		byteArray = tf.getText().getBytes();
			    		bos.write(byteArray, 0, byteArray.length);
			    		bos.flush();
			    		bos.close();
			    		client.close();
					} catch (UnknownHostException e1) {
						
						e1.printStackTrace();
					} catch (Exception e1) {}
						
					finally {
						if (bos != null) {
							try {
								bos.close();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}
						try {
							client.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
							
					}
												
				    }
				    else
				    	if(text.equals("FA2"))
				    {
				    	
						
						int Port = 2222;
										
						try {
							client = new Socket("127.0.0.1", Port);
							
				    		bos = new BufferedOutputStream(client.getOutputStream()); 
				    		byteArray = tf.getText().getBytes();
				    		bos.write(byteArray, 0, byteArray.length);
				    		bos.flush();
				    		bos.close();
				    		client.close();
						} catch (UnknownHostException e1) {
							
							e1.printStackTrace();
						} catch (Exception e1) {}
							
						finally {
							if (bos != null) {
								try {
									bos.close();
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							try {
								client.close();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
								
						}	
				    }
				    	else if(text.equals("FA3"))
				    	{

					    	
							
							int Port = 3333;
											
							try {
								client = new Socket("127.0.0.1", Port);
								
					    		bos = new BufferedOutputStream(client.getOutputStream()); 
					    		byteArray = tf.getText().getBytes();
					    		bos.write(byteArray, 0, byteArray.length);
					    		bos.flush();
					    		bos.close();
					    		client.close();
							} catch (UnknownHostException e1) {
								
								e1.printStackTrace();
							} catch (Exception e1) {}
								
							finally {
								if (bos != null) {
									try {
										bos.close();
									} catch (IOException e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									}
								}
								try {
									client.close();
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
									
							}	
					    
				    	}
					
				    	else if(text.equals("FA4"))
				    	{


					    	
							
							int Port = 4444;
											
							try {
								client = new Socket("127.0.0.1", Port);
								
					    		bos = new BufferedOutputStream(client.getOutputStream()); 
					    		byteArray = tf.getText().getBytes();
					    		bos.write(byteArray, 0, byteArray.length);
					    		bos.flush();
					    		bos.close();
					    		client.close();
							} catch (UnknownHostException e1) {
								
								e1.printStackTrace();
							} catch (Exception e1) {}
								
							finally {
								if (bos != null) {
									try {
										bos.close();
									} catch (IOException e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									}
								}
								try {
									client.close();
								} catch (IOException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
									
							}	
					    
				    		
				    	}
					}
				 
			
					
						
				}
			 
			
			 
	    	
	    
	
	public static void main(String args[])
	{
		new mobileterminal();
	}
}
	
	
	

		


